#include "httplib.h"
#include <iostream>
#include <string>

void Hello(const httplib::Request &req,  httplib::Response &rsp)
{

    rsp.set_content("hello World!", "text/plain");
    rsp.status = 200;


}

void Numbers(const  httplib::Request &req,  httplib::Response &rsp)
{
    std::cout << req.matches[0] << std::endl;
    auto num = req.matches[1]; // 0里面是整体path，往后下标是捕捉的数据

    rsp.set_content(num, "text/plain");
    rsp.status = 200;
}

void Multipart(const  httplib::Request &req,  httplib::Response &rsp)
{
    auto ret = req.has_file("file");
    if (ret == false)
    {
        std::cout << "not file upload" << std::endl;
        rsp.status = 400;
        return;
    }

    const auto& file = req.get_file_value("file");
    rsp.body.clear();
    rsp.body = file.filename;
    rsp.body += "\n";
    rsp.body += file.content;

    rsp.set_header("Content-Type","text/plain");
    rsp.status = 200;

}

int main()
{
    httplib::Server server;

    server.Get("/hi", Hello);
    server.Get(R"(/number/(\d+))", Numbers);
    server.Post("/multipart", Multipart);

    // 0.0.0.0表示监控所有网卡
    server.listen("0.0.0.0", 9090);

    return 0;
}